package com.baseframe.score.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baseframe.score.entity.SnsScoreUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baseframe.score.entity.request.SnsScoreUserRequest;
import com.baseframe.score.entity.vo.SnsScoreUserVO;
import org.apache.ibatis.annotations.*;

/**

* @description 针对表【sns_score_user(用户积分表)】的数据库操作Mapper
* @createDate 2023-06-26 00:45:34
* @Entity com.baseframe.score.entity.SnsScoreUser
*/
public interface SnsScoreUserMapper extends BaseMapper<SnsScoreUser> {


    @Select("<script>" +
            "select ss.*,bu.username from sns_score_user as ss left join base_user as bu on ss.user_id = bu.id" +
            "<where>" +
            "ss.is_deleted = 0" +
            "<if test=\" request.username!=null &amp;&amp; request.username !='' \">" +
            "and bu.username = #{request.username}" +
            "</if>" +
            "</where>" +
            "order by ss.create_time desc" +
            "</script>")
    @Results(
            @Result(column = "username" ,property = "username")
    )
    Page<SnsScoreUserVO> snsScoreUserPage(Page<SnsScoreUserVO> page,@Param("request") SnsScoreUserRequest request);

    @Update("update sns_score_user set accrue_total = accrue_total + #{rewardValue} ,residue = residue + #{rewardValue} where user_id = #{userId}")
    boolean increaseScore(@Param("userId") Long userId,@Param("rewardValue") Integer rewardValue);

    @Select("select ssu.*,bu.username,(select ss.sign_time from sns_sign as ss where ss.user_id = #{userId} order by ss.sign_time desc limit 1 ) as last_sign_in_time  from base_user as bu left join sns_score_user as ssu on bu.id = ssu.user_id where bu.is_deleted = 0 and ssu.is_deleted = 0 and bu.id = #{userId} ")
    SnsScoreUserVO getUserScoreDetailsByUserId(@Param("userId") Long userId);
}




